package com.bonc.modules.job.task;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.bonc.common.utils.ConfigConstant;
import com.bonc.common.utils.HttpUtils;
import com.bonc.common.utils.JsonUtils;
import com.bonc.common.utils.MapUtils;
import com.bonc.modules.business.service.TbWorkOederDistributionService;
import com.bonc.modules.business.service.TbWorkOrderArchiveService;
import com.bonc.modules.business.service.TbWorkOrderCancelService;
import com.bonc.modules.business.service.TbWorkOrderInfoService;
import com.bonc.modules.business.service.TbWorkOrderReproceeService;
import com.bonc.modules.business.service.TbWorkOrderUrgeService;

/**
 * 省OA工单派发
 * @author feiniao
 *
 */
@Component("workOrderTask")
public class WorkOrderDistributionTask implements ITask {
	@Value("${oa.WorkOrderUrl}")
	private String workOrderUrl;
	
	@Autowired
    private TbWorkOrderInfoService tbWorkOrderInfoService;
    @Autowired
    private TbWorkOederDistributionService tbWorkOederDistributionService;
    @Autowired
    private TbWorkOrderCancelService tbWorkOrderCancelService;
    @Autowired
    private TbWorkOrderReproceeService tbWorkOrderReproceeService;
    @Autowired
    private TbWorkOrderUrgeService tbWorkOrderUrgeService;
    @Autowired
    private TbWorkOrderArchiveService tbWorkOrderArchiveService;
	
    @Override
	public void run(String params) {
		List<Map<String,Object>> archiveList = tbWorkOrderArchiveService.queryArchiveList();
    	List<Map<String,Object>> urgeList = tbWorkOrderUrgeService.queryUrgeList();
    	List<Map<String,Object>> cancelList = tbWorkOrderCancelService.queryCancelList();
    	List<Map<String,Object>> reproceeList = tbWorkOrderReproceeService.queryReproceeList();
    	List<Map<String,Object>> distributionList = tbWorkOederDistributionService.queryDistributionList();

		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		list.addAll(distributionList);
		list.addAll(cancelList);
		list.addAll(reproceeList);
		list.addAll(urgeList);
		list.addAll(archiveList);

		for (Map<String, Object> map : list) {
			String identifier = map.get("identifier")==null ? "": (String) map.get("identifier");
			String orderType = map.get("orderType")==null ? "": (String) map.get("orderType");
			String identyType = map.get("identyType")==null ? "": (String) map.get("identyType");//派单必传
			String identySubtype = map.get("identySubtype")==null ? "": (String) map.get("identySubtype");//派单必传
			String title = map.get("title")==null ? "": (String) map.get("title");//派单必传
			String content = map.get("content")==null ? "": (String) map.get("content");//派单必传
			String originUnit = map.get("originUnit")==null ? "": (String) map.get("originUnit");//派单必传
			String receiverUnit = map.get("receiverUnit")==null ? "": (String) map.get("receiverUnit");//派单必传
			
			String extEarlyWarningLevel = map.get("extEarlyWarningLevel")==null ? "": (String) map.get("extEarlyWarningLevel");//0002必传、0003必传
			String dataFileNameList = map.get("dataFileNameList")==null ? "": (String) map.get("dataFileNameList");//005必传、0201必传
			String extEarlyWarningThreshold = map.get("extEarlyWarningThreshold")==null ? "": (String) map.get("extEarlyWarningThreshold");//0006必传
			String filingOpinion = map.get("filingOpinion")==null ? "": (String) map.get("filingOpinion");//归档必传
			String withdrawTime = map.get("withdrawTime")==null ? "": (String) map.get("withdrawTime");//撤单必传
			String withdrawReason = map.get("withdrawReason")==null ? "": (String) map.get("withdrawReason");//撤单必传
			String creator = map.get("creator")==null ? "": (String) map.get("creator");//再处理必传
			String creatorTel = map.get("creatorTel")==null ? "": (String) map.get("creatorTel");//再处理必传
			String reprocessingOpinion = map.get("reprocessingOpinion")==null ? "": (String) map.get("reprocessingOpinion");//再处理必传
			String urgeTime = map.get("urgeTime")==null ? "": (String) map.get("urgeTime");//催办必传
			String urgeReason = map.get("urgeReason")==null ? "": (String) map.get("urgeReason");//催办必传
			String responsible = map.get("responsible")==null ? "": (String) map.get("responsible");

			System.out.println("====================>工单"+identifier);
			
			//指定下游处理人
			if(ConfigConstant.IDENTY_SUB_TYPE_0001.equals(identySubtype)) {
				map.put("creator", "duanxuru");
				map.put("creatorContactInfo", "13903518642");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0003.equals(identySubtype)) {
				map.put("creator", "duanxuru");
				map.put("creatorContactInfo", "13903518642");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0004.equals(identySubtype)) {
				map.put("creator", "liguohong");
				map.put("creatorContactInfo", "13935128109");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0005.equals(identySubtype)) {
				map.put("creator", "duanxuru");
				map.put("creatorContactInfo", "13903518642");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0007.equals(identySubtype)) {
				map.put("creator", "rendongjun");
				map.put("creatorContactInfo", "18803515608");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0101.equals(identySubtype)) {
				map.put("creator", "duanxuru");
				map.put("creatorContactInfo", "13903518642");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0201.equals(identySubtype)) {
				map.put("creator", "duanxuru");
				map.put("creatorContactInfo", "13903518642");
			}else if(ConfigConstant.IDENTY_SUB_TYPE_0202.equals(identySubtype)) {
				map.put("creator", "liguohong");
				map.put("creatorContactInfo", "13935128109");
			}else {
				map.put("creator", "renyaohua");
				map.put("creatorContactInfo", "15234252065");
			}
			
			if(StringUtils.isBlank(identifier) || StringUtils.isBlank(orderType) || StringUtils.isBlank(responsible) ){
				System.out.println("缺少公共必传参数！");
	        }

			if(ConfigConstant.IDENTY_TYPE_00.equals(orderType)) {
				if(StringUtils.isBlank(identyType) ||
						StringUtils.isBlank(identySubtype) ||
						StringUtils.isBlank(title) ||
						StringUtils.isBlank(content) ||
						StringUtils.isBlank(originUnit) ||
						StringUtils.isBlank(receiverUnit)) {
					System.out.println(identifier+"派单缺少必传参数！");
				}
//				if((ConfigConstant.IDENTY_SUB_TYPE_0002.equals(identySubtype) || ConfigConstant.IDENTY_SUB_TYPE_0003.equals(identySubtype)) && StringUtils.isBlank(extEarlyWarningLevel)) {
//					System.out.println(identifier+"(0002产品、003产品)缺少必传参数！");
//				}
//				if(ConfigConstant.IDENTY_SUB_TYPE_0005.equals(identySubtype) && StringUtils.isBlank(dataFileNameList)) {
//					System.out.println(identifier+"(0005产品)缺少必传参数！");
//				}
//				if(ConfigConstant.IDENTY_SUB_TYPE_0006.equals(identySubtype) && StringUtils.isBlank(extEarlyWarningThreshold)) {
//					System.out.println(identifier+"(0006产品)缺少必传参数！");
//				}
			}

			if(ConfigConstant.IDENTY_SUB_TYPE_0201.equals(identySubtype) && StringUtils.isBlank(dataFileNameList)) {
				System.out.println(identifier+"(0201产品)缺少必传参数！");
			}

			if(ConfigConstant.ORDER_TYPE_01.equals(orderType) && (StringUtils.isBlank(withdrawTime) || StringUtils.isBlank(withdrawReason))) {
				System.out.println(identifier+"撤单缺少必传参数！");
			}

			if(ConfigConstant.ORDER_TYPE_02.equals(orderType) && (StringUtils.isBlank(creator) || StringUtils.isBlank(creatorTel) || StringUtils.isBlank(reprocessingOpinion))) {
				System.out.println(identifier+"再处理缺少必传参数！");
			}

			if(ConfigConstant.ORDER_TYPE_03.equals(orderType) && (StringUtils.isBlank(urgeTime) || StringUtils.isBlank(urgeReason))) {
				System.out.println(identifier+"催办缺少必传参数！");
			}

			if(ConfigConstant.ORDER_TYPE_04.equals(orderType) && StringUtils.isBlank(filingOpinion)) {
				System.out.println(identifier+"归档缺少必传参数！");
			}
			
			map  = MapUtils.nullToEmpty(map);
			String json = JsonUtils.map2String(map);
			String result = HttpUtils.doPost(workOrderUrl, json);
			System.out.println(json);
			System.out.println(result);
			//修改省内处理状态
			Map<String, Object> resultMap = MapUtils.json2Map(result);
			if("1".equals((String)resultMap.get("state"))) {
				tbWorkOrderInfoService.updateHandingStatus(identifier);
			}
		}
		System.out.println("================派单完成=======================");
	}

}
